package com.jubao.ejb.dao.tests;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.JUnit4TestAdapter;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public class CategoryXml2FlatText {
	static Logger logger = Logger.getLogger(CategoryXml2FlatText.class);

	private String fileHome = "D:/conanwork/fatcat-workspace/Com_NiuMoWang_EJB/data/category/";

	// private String[] categoryFileNames = {
	// "category/area_category_shanghai.xml" };
	// private String[] categoryFileNames = {
	// "category/area_category_guangdong.xml" };

	// private String[] categoryFileNames = {
	// "category/area_category_beijing.xml" };

	// private String[] categoryFileNames = { "category/area_category_wuhan.xml"
	// };

	// private String[] categoryFileNames = {
	// "category/area_category_tianjing.xml" };
	// private String[] categoryFileNames = {
	// "category/area_category_nanjing.xml" };
	// private String[] categoryFileNames = { "category/book_category.xml" };
	// private String[] categoryFileNames = { "category/brand_category.xml" };
	// private String[] categoryFileNames = {
	// "category/product_musicvideo_category.xml" };
	// private String[] categoryFileNames = {
	// "category/product_electronic_category.xml" };
	private String[] categoryFileNames = { 
			"website_category_google.xml"
	};

	//private String[] categoryFileNames = { "category/business_category.xml" };

	public CategoryXml2FlatText() {
		super();
	}

	public static void main(String... args) {
		junit.textui.TestRunner.run(suite());
	}

	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(CategoryXml2FlatText.class);
	}

	@Test
	// step 1
	public void testImportCategories() {
		for (int i = 0; i < categoryFileNames.length; i++) {
			importDataFromFile(fileHome + categoryFileNames[i]);
		}
	}

	private void importDataFromFile(String fileName) {
		if (logger.isInfoEnabled()) {
			logger.info("Loading properties file from " + fileName);
		}
		logger.info("Loading properties file from " + fileName);

		// Read from an input stream
		try {
			InputStream is = new BufferedInputStream(new FileInputStream(fileName));
			logger.info("The sequence of records is very important,that means the previous one will be saved first "
					+ "and the later ones maybe depends on the previous one. "
					+ "Entity name first character must be capitalized and the fields must be lowcase");
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(is);
			treeWalk(document);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

	private void treeWalk(Element element, String pCategoryId, String pType) {
		int jj = 1;
		for (int i = 0, size = element.nodeCount(); i < size; i++) {
			Node node = element.node(i);
			if (node instanceof Element) {
				String categoryId = ((Element) node).attributeValue("categoryId");
				if (pCategoryId != null) {
					String ii = String.format("%1$02d", jj);
					categoryId = pCategoryId + ii;
				}

				String name = ((Element) node).attributeValue("name");
				if (name != null)
					name = name.trim();
				String fullName = ((Element) node).attributeValue("fullName");
				if (fullName != null)
					fullName = fullName.trim();
				String type = ((Element) node).attributeValue("type");
				type = type == null ? pType : type.trim();

				System.out.println(categoryId + "|" + pCategoryId + "|" + name + "|" + fullName);
				treeWalk((Element) node, categoryId, type);
				jj++;
			} else {
				// do something....
			}
		}
	}

	private void treeWalk(Document document) {
		treeWalk(document.getRootElement(), null, null);
	}

	/**
	 * Import data from text
	 * 
	 * @param text
	 * @throws IOException
	 * @throws DocumentException
	 */
	public void importDataFromText(String text) throws IOException, DocumentException {
		// If you have some XML as a String you can parse it back into a
		// Document again using the helper method DocumentHelper.parseText()

		Document document = DocumentHelper.parseText(text);
		treeWalk(document);
	}

}